/*
#ifndef LOCKER_HPP
#define LOCKER_HPP
#include <iostream>
#include <exception>
#include <semaphore.h>

class sem {
public:
    sem() {
        if(sem_init(&m_sem, 0, 0) != 0) {
            throw std::exception();
        }
    }
    ~sem() {
        sem_destory(&m_sem);
    }

    bool wait() {
        return sem_wait(&m_sem) == 0;
    }

    bool post() {
        return sem_post(&m_sem) == 0;
    }

private:
    sem_t m_sem;
};

class locker {
public:
    locker() {
        if(pthread_mutex_init(&m_mutex, nullptr) != 0) {
            throw std::exception();
        }
    }
    ~locker() {
        pthread_mutex_destroy(&m_mutex);
    }

    bool lock() {
        return pthread_mutex_lock(&m_mutex) == 0;
    }

    bool unlock() {
        return pthread_mutex_unlock(&m_mutex) == 0;
    }

private:
    pthread_mutex_t m_mutex;
};

class cond {
public:
    cond() {
        if(pthread_mutex_init(&m_mutex, nullptr) != 0) {
            throw std::exception();
        }
        if(pthread_cond_init(&m_cond, nullptr) != 0) {
            pthread_mutex_destroy(&m_mutex);
            throw std::exception();
        }
    }
    ~cond() {
        pthread_mutex_destroy(&m_mutex);
        pthread_cond_destroy(&m_cond);
    }

    bool wait() {
        int ret = 0;
        pthread_mutex_lock(&m_mutex);
        ret = pthread_cond_wait(&m_cond, &m_mutex);
        pthread_mutex_unlock(&m_mutex);
        return ret == 0;
    }

    bool signal() {
        return pthread_cond_signal(&m_cond) == 0;
    }

private:
    pthread_mutex_t m_mutex;
    pthread_cond_t m_cond;
}

#endif // LOCKER_HPP
*/

#ifndef LOCKER_HPP
#define LOCKER_HPP

#include <mutex>
#include <condition_variable>
#include <semaphore>  // C++20 起支持，需编译器支持

// 信号量类（使用 C++20 标准库）
class Semaphore {
public:
    explicit Semaphore(unsigned int count = 0) : m_semaphore(count) {}
    
    void wait() { m_semaphore.acquire(); }
    void post() { m_semaphore.release(); }

private:
    std::counting_semaphore<UINT_MAX> m_semaphore;
};

// 互斥锁类（使用 RAII 自动管理锁）
class Locker {
public:
    Locker() = default;
    ~Locker() = default;

    void lock() { m_mutex.lock(); }
    void unlock() { m_mutex.unlock(); }

    // 提供锁的原生句柄（如果需要与其他 API 交互）
    pthread_mutex_t* native_handle() { return m_mutex.native_handle(); }

private:
    std::mutex m_mutex;
};

// 条件变量类（结合互斥锁实现等待/通知机制）
class Condition {
public:
    Condition() = default;
    ~Condition() = default;

    // 等待条件满足
    template<typename Predicate>
    void wait(std::unique_lock<std::mutex>& lock, Predicate pred) {
        m_cond.wait(lock, std::move(pred));
    }

    void wait(std::unique_lock<std::mutex>& lock) {
        m_cond.wait(lock);
    }

    // 通知机制
    void notify_one() noexcept { m_cond.notify_one(); }
    void notify_all() noexcept { m_cond.notify_all(); }

private:
    std::condition_variable m_cond;
};

// 读写锁（共享锁）实现（C++17 起支持）
class ReadWriteLock {
public:
    void readLock() { m_rwlock.lock_shared(); }
    void readUnlock() { m_rwlock.unlock_shared(); }
    
    void writeLock() { m_rwlock.lock(); }
    void writeUnlock() { m_rwlock.unlock(); }

private:
    std::shared_mutex m_rwlock;
};

// RAII 风格的锁守卫（自动加锁/解锁）
class LockGuard {
public:
    explicit LockGuard(Locker& locker) : m_locker(locker) {
        m_locker.lock();
    }
    
    ~LockGuard() {
        m_locker.unlock();
    }

    // 禁止拷贝和移动
    LockGuard(const LockGuard&) = delete;
    LockGuard& operator=(const LockGuard&) = delete;

private:
    Locker& m_locker;
};

#endif // LOCKER_HPP    